package cscie160.project.atm;
/**
 * This class is in atm package because it is used by the ATM.
 * First thing the ATM does upon receiving a transaction request, it notifies 
 * each of its listeners with a TransactionNotification 
 * indicating that it is about to process the operation.
 * @author Michael Melnikov  
*/
public class TransactionNotification implements java.io.Serializable
{
    private cscie160.project.bank.Commands command;
    private cscie160.project.bank.AccountInfo sourceAccountInfo;
    private cscie160.project.bank.AccountInfo targetAccountInfo;
    private Integer amount;
    
    /**
    * no-args constructor.
    * Assigns command to NOTHING.
    * This command is not an executable one.
    */
    public TransactionNotification()
    {
        this(cscie160.project.bank.Commands.NOTHING);
    }

    /**
    * 1-args constructor.
    * Assigns command to its argument
    * @param command cscie160.project.bank.Commands 
    */
    public TransactionNotification(cscie160.project.bank.Commands command)
    {
        this(command, null, null, null);
    }

    /**
    * 4-args constructor.
    * Assigns all the fields accourding to its argument
    * @param command cscie160.project.bank.Commands 
    * @param sourceAccountInfo cscie160.project.bank.AccountInfo 
    * @param targetAccountInfo cscie160.project.bank.AccountInfo 
    * @param amount Integer 
    */
    public TransactionNotification(cscie160.project.bank.Commands command,
        cscie160.project.bank.AccountInfo sourceAccountInfo,
        cscie160.project.bank.AccountInfo targetAccountInfo,
        Integer amount)
    {
        this.command = command;
        this.sourceAccountInfo = sourceAccountInfo;
        this.targetAccountInfo = targetAccountInfo;
        this.amount = amount;
    }
 
    /**
    * returns the command of this transaction
    * @return cscie160.project.bank.Commands 
    */
    public cscie160.project.bank.Commands getCommand()
    {
        return command;
    }

    /**
    * returns the SourceAccountInfo of this transaction
    * @return cscie160.project.bank.AccountInfo 
    */
    public cscie160.project.bank.AccountInfo getSourceAccountInfo()
    {
        return sourceAccountInfo;
    }

    /**
    * returns the TargetAccountInfo of this transaction
    * @return cscie160.project.bank.AccountInfo 
    */
    public cscie160.project.bank.AccountInfo getTargetAccountInfo()
    {
        return targetAccountInfo;
    }
   
    /**
    * returns the amount of this transaction
    * @return Integer 
    */
    public Integer getAmount()
    {
        return amount;
    }

    /**
    * provides string representation of the command
    * @return String 
    */
    public String toString()
    {
        return toString(true);
    }

    /**
    * provides string representation of the required transaction.
    * output comprises the entire information or command only depending on boolean parameter
    * @param isFullRepresentation boolean
    * @return String 
    */
    public String toString(boolean isFullRepresentation)
    {
        StringBuffer sb = new StringBuffer(command.toString());
        if (! isFullRepresentation)
            return sb.toString();

        sb.append("; sourceAccountInfo={");
        if (sourceAccountInfo != null)
        {
            sb.append("accountNumber=");
            sb.append(Integer.toString(sourceAccountInfo.getAccountNumber()));
            sb.append("; pin=");
            sb.append(Integer.toString(sourceAccountInfo.getPin()));
        }
        else
            sb.append("NULL");
        sb.append("}");

        if (targetAccountInfo != null)
        {
            sb.append("; targetAccountInfo={");
            sb.append("accountNumber=");
            sb.append(Integer.toString(targetAccountInfo.getAccountNumber()));
            sb.append("; pin=");
            sb.append(Integer.toString(targetAccountInfo.getPin()));
            sb.append("}");
        }

        if (amount != null)
        {
            sb.append("; amount=");
            sb.append(Integer.toString(amount));
        }    
        return sb.toString();
    }    
}